#![no_std]
#![no_main]
#![allow(named_asm_labels)]
#![feature(pointer_is_aligned_to)]
#![feature(naked_functions)]
#![feature(ptr_metadata)]
extern crate alloc;

mod io;
mod kernel_hal;
#[macro_use]
mod logging;
mod example;
mod homework;
mod mimalloc;
mod uart;

use crate::example::{example3_2, example3_3, example3_5, example3_7};
use crate::homework::{h3_14_1, h3_14_3, h3_14_4, h3_14_5, h3_14_6, h3_14_7};
use core::arch::asm;
use core::{arch::global_asm, panic::PanicInfo};

global_asm!(include_str!("boot.s"));

#[panic_handler]
fn panic_handler(_info: &PanicInfo) -> ! {
    loop {}
}

#[unsafe(no_mangle)]
fn kernel_main() {
    println!("hello world, 欢迎来到我的世界!");
    example3_2();
    example3_3();
    example3_5();
    unsafe {
        example3_7();
    }
    println!("homework-------------------------");
    h3_14_1();
    h3_14_3();
    let mut buf = [12u8; 32];

    h3_14_4(&mut buf[0], 15, 10);

    println!("写入后的结果为: {:?}", &buf[..10]);
    let ret = h3_14_5(10, 2);
    println!("比较结果为: 0x{:x}", ret);
    let ret = h3_14_5(1, 2);
    println!("比较结果为: 0x{:x}", ret);
    unsafe {
        h3_14_7(10);
        let ret: usize;
        // 调用sel_test函数
        asm! (
            "li a0, 0",
            "li a1, 20",
            "call sel_test",
            "mv {0}, a0",
            out(reg) ret
        );
        println!("ret: {}", ret);
    }
    loop {
        continue;
    }
}
